package air2.totc.common;

import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class GestionBD extends SQLiteOpenHelper {
	private static final String DATABASE_NAME = "totc.db";
	private static final int DATABASE_VERSION = 1;
	
	//colonnes de la table des clés
	public static final String COLUMN_ID_CLE = "cle_id";
	public static final String COLUMN_CLE = "cle";	
	public static final String COLUMN_COMMENTAIRE = "commentaire";	
	
	//colonnes de la table des sms 
	public static final String COLUMN_ID_SMS = "sms_id";
	public static final String COLUMN_SMS = "corps_sms";
	public static final String COLUMN_ETAT = "etat";
	
	//colonnes "communes" aux deux tables
	public static final String COLUMN_ID_CONTACT = "contact_id";
	public static final String COLUMN_CRYPTO = "type_crypto";
	
	// noms des tables
	public static final String TABLE_TOTCSMS="TOTCDBSMS";
	public static final String TABLE_TOTCCLES="TOTCDBCLES";
	
	String createTableSMS = "CREATE TABLE "+TABLE_TOTCSMS+" ( "+
			COLUMN_ID_SMS+" INTEGER PRIMARY KEY autoincrement, " +
			COLUMN_ID_CONTACT +" INTEGER, "+ // si contact = 0 alors il n'est pas dans la BDD des contacts
			COLUMN_SMS+" TEXT NOT NULL, "+
			COLUMN_CRYPTO+" INTEGER NOT NULL,"+
			COLUMN_ETAT+" INTEGER );";
	
	String createTableCLES = "CREATE TABLE "+TABLE_TOTCCLES+" ( "+
			COLUMN_ID_CLE+" INTEGER PRIMARY KEY autoincrement, " +
			COLUMN_ID_CONTACT +" INTEGER , "+ // si contact = 0 alors il n'est pas dans la BDD des contacts 
			COLUMN_CLE+" TEXT NOT NULL, "+
			COLUMN_CRYPTO+" INTEGER NOT NULL, "+
			COLUMN_COMMENTAIRE+" TEXT );";
	
	public GestionBD(Context context) {
		super(context, DATABASE_NAME, null, DATABASE_VERSION);
	}

	@Override
	public void onCreate(SQLiteDatabase smsDB) {
		smsDB.execSQL(createTableSMS);
		smsDB.execSQL(createTableCLES);
	}
   
	@Override
	public void onUpgrade(SQLiteDatabase smsDB, int oldVersion, int newVersion) {
			smsDB.execSQL("DROP TABLE IF EXISTS "+TABLE_TOTCSMS);
			smsDB.execSQL("DROP TABLE IF EXISTS "+TABLE_TOTCCLES);
			onCreate(smsDB);
	}


}
